<template>
  <div class="theme-view">
    <div class="view-header">
      <h2 class="view-title">主题设置</h2>
      <p class="view-subtitle">自定义您的应用外观</p>
    </div>

    <div class="theme-content">
      <!-- 主题模式选择 -->
      <div class="theme-section">
        <h3 class="section-title">主题模式</h3>
        <div class="theme-options">
          <div
            class="theme-option"
            :class="{ active: uiStore.themeSource === 'system' }"
            @click="setThemeSource('system')"
          >
            <div class="theme-preview system-preview">
              <div class="preview-header"></div>
              <div class="preview-content">
                <div class="system-icon"></div>
              </div>
            </div>
            <span class="theme-name">跟随系统</span>
          </div>

          <div
            class="theme-option"
            :class="{ active: uiStore.themeSource === 'light' }"
            @click="setThemeSource('light')"
          >
            <div class="theme-preview light-preview">
              <div class="preview-header"></div>
              <div class="preview-content"></div>
            </div>
            <span class="theme-name">浅色主题</span>
          </div>

          <div
            class="theme-option"
            :class="{ active: uiStore.themeSource === 'dark' }"
            @click="setThemeSource('dark')"
          >
            <div class="theme-preview dark-preview">
              <div class="preview-header"></div>
              <div class="preview-content"></div>
            </div>
            <span class="theme-name">深色主题</span>
          </div>
        </div>
      </div>

      <!-- 颜色自定义 -->
      <div class="theme-section">
        <h3 class="section-title">主色调</h3>
        <div class="color-palette">
          <div
            v-for="color in colorOptions"
            :key="color.name"
            class="color-option"
            :class="{ active: selectedColor === color.value }"
            :style="{ backgroundColor: color.value }"
            @click="selectColor(color.value)"
          >
            <span class="color-name">{{ color.name }}</span>
          </div>
        </div>
      </div>

      <!-- 字体设置 -->
      <div class="theme-section">
        <h3 class="section-title">字体设置</h3>
        <div class="font-options">
          <div class="font-option">
            <label>字体大小</label>
            <input v-model="fontSize" type="range" min="12" max="18" class="font-slider" />
            <span class="font-size-display">{{ fontSize }}px</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useUIStore } from '@/store/ui'
import { useThemeTool } from '@/composables/useThemeTool'

const uiStore = useUIStore()
const { setThemeSource } = useThemeTool()

const selectedColor = ref('#349eff')
const fontSize = ref(14)
const currentThemeStatus = ref('当前使用系统主题')

const colorOptions = [
  { name: '蓝色', value: '#349eff' },
  { name: '绿色', value: '#4FC08D' },
  { name: '紫色', value: '#9C27B0' },
  { name: '橙色', value: '#FF9800' },
  { name: '红色', value: '#F44336' },
  { name: '青色', value: '#00BCD4' }
]

onMounted(() => {
  updateThemeStatus()
})

const updateThemeStatus = (): void => {
  if (uiStore.themeSource === 'system') {
    currentThemeStatus.value = `当前使用系统主题 (${uiStore.theme === 'dark' ? '深色' : '浅色'})`
  } else {
    currentThemeStatus.value = `当前使用${uiStore.theme === 'dark' ? '深色' : '浅色'}主题`
  }
}

const selectColor = (color: string): void => {
  selectedColor.value = color
  // 保存主色调设置
  localStorage.setItem('primary-color', color)
  document.documentElement.style.setProperty('--theme-primary', color)
  console.log('设置主色调:', color)
}
</script>

<style scoped>
.theme-view {
  height: 100%;
  padding: 24px;
  background-color: var(--theme-bg-primary);
  overflow-y: auto;
}

.view-header {
  margin-bottom: 32px;
}

.view-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--theme-text-primary);
  margin-bottom: 8px;
}

.view-subtitle {
  color: var(--theme-text-secondary);
  font-size: 14px;
}

.theme-content {
  max-width: 600px;
}

.theme-section {
  margin-bottom: 32px;
}

.section-title {
  font-size: 18px;
  font-weight: 500;
  color: var(--theme-text-primary);
  margin-bottom: 16px;
}

.theme-options {
  display: flex;
  gap: 16px;
}

.theme-option {
  flex: 1;
  padding: 16px;
  border: 2px solid var(--theme-border-light);
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  text-align: center;
}

.theme-option:hover {
  border-color: var(--theme-primary);
}

.theme-option.active {
  border-color: var(--theme-primary);
  background-color: rgba(var(--theme-primary-rgb, 51, 112, 255), 0.1);
}

.theme-preview {
  width: 100%;
  height: 80px;
  border-radius: 8px;
  margin-bottom: 12px;
  overflow: hidden;
}

.system-preview {
  background: linear-gradient(to right, #ffffff 50%, #1a1a1a 50%);
  border: 1px solid #e0e0e0;
  position: relative;
}

.system-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: var(--theme-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

.system-icon:before {
  content: '⚙️';
  font-size: 16px;
}

.light-preview {
  background-color: #ffffff;
  border: 1px solid #e0e0e0;
}

.dark-preview {
  background-color: #1a1a1a;
  border: 1px solid #333333;
}

.preview-header {
  height: 20px;
  background-color: var(--theme-bg-secondary);
}

.preview-content {
  height: 60px;
  background-color: var(--theme-bg-primary);
  display: flex;
  align-items: center;
  justify-content: center;
}

.theme-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--theme-text-primary);
}

.color-palette {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 12px;
}

.color-option {
  height: 60px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

.color-option:hover {
  transform: scale(1.05);
}

.color-option.active {
  border-color: var(--theme-text-primary);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.3);
}

.color-name {
  color: white;
  font-weight: 500;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.font-options {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.font-option {
  display: flex;
  align-items: center;
  gap: 16px;
}

.font-option label {
  font-weight: 500;
  color: var(--theme-text-primary);
  min-width: 80px;
}

.font-slider {
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: var(--theme-border-light);
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.font-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--theme-primary);
  cursor: pointer;
}

.font-slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--theme-primary);
  cursor: pointer;
  border: none;
}

.font-size-display {
  min-width: 40px;
  text-align: right;
  color: var(--theme-text-secondary);
  font-size: 14px;
}
</style>
